<!--
 * @Author: wangming
 * @Date: 2020-09-09 14:04:55
 * @LastEditors: wangming
 * @LastEditTime: 2021-05-08 17:42:58
 * @Description:添加类别按钮
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="660px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>自定义选项</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="FormData"
      inline
      ref="Form"
      label-position="left"
    >
      <div
        class="ks-row-middle"
        v-for="(domain, index) in FormData.list"
        :key="index"
      >
        <el-form-item class="nowrap mr10">选项 {{ index + 1 }}:</el-form-item>
        <el-form-item
          class="ks-col"
          label="名称"
          :prop="'list.' + index + '.label'"
          :rules="[
            { required: true, message: '名称不能为空', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="domain.label"
            placeholder="请输入选项名称"
            class="ml10"
          ></el-input>
        </el-form-item>
        <el-form-item
          class="ks-col"
          label="ID"
          :prop="'list.' + index + '.value'"
          :rules="[{ required: true, message: 'ID不能为空', trigger: 'blur' }]"
        >
          <el-input
            v-model="domain.value"
            placeholder="请输入选项名称"
            class="ml10"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="FormData.list.splice(index, 1)"
            type="text"
            icon="el-icon-delete"
            v-if="FormData.list.length > 1"
            >删除</el-button
          ></el-form-item
        >
      </div>

      <div>
        <el-button
          @click="
            FormData.list.push({
              label: '自定义选项' + (FormData.list.length + 1),
              value: FormData.list.length + 1 + '',
            })
          "
          icon="el-icon-plus"
          >添加选项</el-button
        >
      </div>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { cloneDeep } from 'lodash';
export default {
  data() {
    return {
      FormData: {
        list: [],
      },
      showVisible: false,
      callback: () => {},
    };
  },
  methods: {
    editForm(list) {
      this.FormData.list = cloneDeep(list);
      return this.showModel();
    },
    showModel() {
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === 'function') {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.Form.validate();
        this.showVisible = false;
        this.callback(this.FormData.list);
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped></style>
